package com.bocai.kelala.ui.travel.set.presenter;

import android.util.Log;

import com.bocai.kelala.bean.TravelInformationBean;
import com.bocai.kelala.bean.UploadImgBean;
import com.bocai.kelala.ui.travel.set.contract.AddAddressContract;
import com.bocai.kelala.ui.travel.set.contract.CertificationContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/21.
 */

public class CertificationPresenter extends CertificationContract.presenter {

    private final CertificationContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public CertificationPresenter(CertificationContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void travelInfor(String memberId, String CompanyName, String ContactName, String IdNumber, String LicenseId) {
        api.travelInfor(memberId,CompanyName,ContactName,IdNumber,LicenseId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                   view.travelInforSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void uploadImg(List<String> photos) {
        view.showLoading("正在上传...");
        List<MultipartBody.Part> parts = new ArrayList<>();
        MultipartBody.Builder builder=new MultipartBody.Builder();
        Map<String, RequestBody> map = new HashMap<>();
        for (int i = 0;i < photos.size();i++){
            File file = new File(photos.get(i));
            map.put("file"+"\";filename =\""+ file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"),file));
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
            builder.addFormDataPart("file",file.getName(),requestBody);
            parts.add(builder.build().part(i));
        }
        api.uploadImage(parts).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
                    Log.e("result",result);
                    UploadImgBean uploadImgBean = new Gson().fromJson(result,UploadImgBean.class);
                    view.uploadImgSuccess(uploadImgBean);
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getTravelInfor(String memberId) {
        api.getTravelInfor(memberId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){

                    String result = AESUtils.decode(baseResponse.getData());
                    TravelInformationBean bean = new Gson().fromJson(result,TravelInformationBean.class);
                    view.getTravelSuccess(bean);
                    Log.e("result",result);

                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
